<?php

namespace app\stock\controller;

use think\Controller;

class Visit extends Controller
{

    // 用户模型
    protected $BusinessModel;
    protected $VisitModel;
    public function _initialize()
    {
        // 继承父类
        parent::_initialize();
        $this->BusinessModel = model('Business.Business');
        $this->VisitModel = model('Business.Visit');
    }
    
    public function index()
    {
        $adminid = $this->request->param('adminid', 0, 'trim');
        $data=$this->VisitModel->with('admin')->where('adminid', $adminid)->select();
        $this->success("","",$data);
    }
    public function add()
    {
        $adminid = $this->request->param('adminid', 0, 'trim');
        $content = $this->request->param('content', '', 'trim');
        $busid = $this->request->param('busid', 0, 'trim');
        $result=$this->VisitModel->validate("common/Business/Visit")->save(['adminid'=>$adminid, 'busid'=>$busid, 'content'=>$content]);
        if ($result === false) {
            $this->error($this->VisitModel->getError());
        }
        $this->success("添加成功");
    }
    public function business()
    {
        $adminid = $this->request->param('adminid', 0, 'trim');
        $data=$this->BusinessModel->where('adminid', $adminid)->select();
        $this->success("","",$data);
    }
    // 详情
    public function info(){
        $id = $this->request->param('id', 0, 'trim');
        $info=$this->VisitModel->with('business')->find($id);
        if (!$info) {
            $this->error("回访记录不存在");
        }
        $this->success("","",$info);
    }
    // 删除
    public function del(){
        $id = $this->request->param('id', 0, 'trim');
        $source= $this->VisitModel->find($id);
        if (!$source) {
            $this->error("回访记录不存在");
        }
        $result =  $this->VisitModel->destroy($id);
        if (!$result) {
            $this->error("删除失败！");
        }
        $this->success("删除成功");
    }
     /**
     * 修改
     */
    public function edit()
    {
        $id = $this->request->param('id', 0, 'trim');
        $adminid = $this->request->param('adminid', 0, 'trim');
        $content = $this->request->param('content', '', 'trim');
        $busid = $this->request->param('busid', 0, 'trim');
        $source=$this->VisitModel->find($id);
        if (!$source) {
            $this->error("回访记录不存在");
        }
        $result=$this->VisitModel->validate("common/Business/Visit")->isUpdate()->save(['adminid'=>$adminid, 'busid'=>$busid, 'content'=>$content,'id'=>$id]);
        if ($result===false) {
            $this->error($this->VisitModel->getError());
        }
        $this->success("修改成功");
    }
}
